<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <title>Document</title>
</head>

<body>
    <iframe id='test' frameborder="0"></iframe>
    <button onclick="handleClick()">click me </button>
</body>
<script>
    function Handle(arr) {
        // 初始条件
        let rol = arr.length
        let col = arr[0].length
        let f = new Array(rol).fill(0)
        for (let i = 0; i < rol; i++) {
            f[i] = new Array(col).fill(0)
        }
        f[0][0] = arr[0][0]
        // f[m][n]表示 达到m-1,n-1所需的最小开销
        // 转移方程 f(m,n) = min(f(m-1.n),f(m,n-1))
        let i
        let j
        for (i = 0; i < rol; i++) {
            for (j = 0; j < col; j++) {
                // 边界处理 起点跳过
                if (i === 0 && j === 0) continue
                // 边界处理墙走不通,设置为最大
                if (arr[i][j] === 0) {
                    f[i][j] = Number.MAX_VALUE
                    continue
                } 
                // 边界处理 最外层只能直走
                if (i === 0 || j === 0) {
                    if (i === 0) f[i][j] = f[i][j - 1] + arr[i][j]
                    else f[i][j] = f[i - 1][j] + arr[i][j]
                }else {
                        f[i][j] = Math.min(f[i - 1][j], f[i][j - 1]) + arr[i][j]
                    }
            }
        }
        return f[i - 1][j - 1]
    }
    let arr = [
        [1, 2, 6, 0],
        [3, 0, 5, 0],
        [4, 5, 6, 2]
    ]
    // 1 2 6 5 6 2
    // 1 3 4 5 6 2
    console.log(Handle(arr))
</script>

</html>